package com.ld.admin.service.system;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import com.ld.admin.model.system.SysSensitiveWordModel;
import com.ld.shieldsb.common.core.model.PageBean;
import com.ld.shieldsb.common.core.model.Result;
import com.ld.shieldsb.common.core.reflect.ClassUtil;
import com.ld.shieldsb.common.core.reflect.ModelUtil;
import com.ld.shieldsb.common.core.util.StringUtils;
import com.ld.shieldsb.common.web.BasicService;
import com.ld.shieldsb.dao.model.PageNavigationBean;
import com.ld.shieldsb.dao.model.QueryModel;
import com.ld.shieldsb.sensitiveword.Dictionary;
import com.ld.shieldsb.sensitiveword.DictionaryItem;
import com.ld.shieldsb.sensitiveword.service.SensitiveWordService;

import lombok.extern.slf4j.Slf4j;

/**
 * 敏感词处理器工具(数据库服务)
 * 
 * @author <a href="mailto:donggongai@126.com" target="_blank">吕凯</a>
 * @date 2019年10月28日 上午8:41:08
 *
 */
@Slf4j
public class SensitiveWordDBService extends SensitiveWordService {
    public static final BasicService service = new BasicService();

    @Override
    public void loadDict() {
        loadDict(new ArrayList<String>());
    }

    @Override
    public void loadDict(List<String> dicts) {
        QueryModel queryModel = service.getNormalQueryModel();
        List<SysSensitiveWordModel> allSendWords = service.getAll(SysSensitiveWordModel.class, queryModel);
        // 加载词库到工具类
        Set<String> sensitiveWords = allSendWords.stream().filter(model -> StringUtils.isNotEmpty(model.getWord()))
                .map(SysSensitiveWordModel::getWord).collect(Collectors.toSet());
        // 替换库
        Map<String, String> replaceWordMap = allSendWords.stream()
                .filter(model -> (model.getType() != null && model.getType() == 1) && StringUtils.isNotEmpty(model.getWord()))
                .collect(Collectors.toMap(SysSensitiveWordModel::getWord, SysSensitiveWordModel::getReplaceWord));
        handler.init(sensitiveWords, replaceWordMap);

    }

    @Override
    public String getDictName(String dictName) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void updateDict(File file) {
        // TODO Auto-generated method stub

    }

    @Override
    public void loadDictionary(Dictionary dict) {
        // TODO Auto-generated method stub

    }

    @Override
    public void removeDictionary(Dictionary dict) {
        // TODO Auto-generated method stub

    }

    @Override
    public Dictionary getDictionary(String dictName) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Dictionary getDictionary(File dictFile) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<Dictionary> getDictsEmbedAll() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<Dictionary> getDictsEmbedLoad() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<Dictionary> getDictsExtAll() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<Dictionary> getDictsExtLoad() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Result delDictItem(String dictFileName, String word, Map<String, Object> extendModel) {
        Long id = ClassUtil.obj2long(word);
        Long userId = (Long) extendModel.get("userId");
        String userName = (String) extendModel.get("userName");
        Result result = service.remove(SysSensitiveWordModel.class, id, userId, userName);
        if (result.getSuccess()) {
            loadDict();
        }
        return result;
    }

    @Override
    public Result addDictItem(String dictFileName, DictionaryItem item, Map<String, Object> extendModel) {
        Result result = new Result();
        result.setMessage("添加失败！");
        if (extendModel != null) {
            SysSensitiveWordModel model = (SysSensitiveWordModel) extendModel.get("model");
            Long userId = (Long) extendModel.get("userId");
            String userName = (String) extendModel.get("userName");
            if (model != null) {
                result = service.addOrUpdate(model, model.getId() != null, userId, userName);
                if (result.getSuccess()) {
                    loadDict();
                }
            } else {
                result.setMessage("无数据！");
            }
        } else {
            result.setMessage("无数据！");
        }
        return result;
    }

    @Override
    public PageBean<DictionaryItem> getDictionaryItemPageBean(DictionaryItem itemQueryModel, Map<String, Object> extendQueryModel,
            int pageNum, int pageSize) {
        QueryModel queryModel = (QueryModel) extendQueryModel.get("queryModel");
        PageNavigationBean<SysSensitiveWordModel> pageBean = service.getPageBean(SysSensitiveWordModel.class, queryModel);
        PageBean<DictionaryItem> pageBeanResult = new PageBean<>();
        ModelUtil.coptyModel(pageBean, pageBeanResult);
        log.warn(pageBeanResult + "");
        return pageBeanResult;
    }

}
